package com.penghaisoft.microwms.base.web.auth.service.impl;

import com.penghaisoft.microwms.base.web.auth.service.IRoleResService;
import com.penghaisoft.microwms.base.web.resrole.dao.BaseResRoleResourceMapper;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseResRoleResourceKey;
import com.penghaisoft.microwms.common.dto.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Title: RoleServiceImpl.java
 * @Description: 角色资源关联服务实现
 * @author qh
 * @date 2017/3/1 16:24
 * @version V0.5
 */
@Service
public class RoleResServiceImpl implements IRoleResService {
	
	@Autowired
	private BaseResRoleResourceMapper roleResourceMapper;

	/**
	 * @Description: 根据角色Id查询关联资源
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param  (资源角色Id)
	 * @return List<BaseResRoleResourceKey> (角色资源关联关系对象列表)
	 */
	@Override
	public List<BaseResRoleResourceKey> queryByRoleId(String resroleId) {
		List<BaseResRoleResourceKey> resRoleResources = roleResourceMapper.selectByResRoleId(resroleId);
		return resRoleResources;
	}

	/**
	 * @Description: 批量更新角色资源关联信息
	 * @author qh
	 * @date 2017/3/1 16:24
	 * @param List<String> (角色Id列表); List<BaseResRoleResourceKey> (角色资源关系列表)
	 * @return Resp (结果对象)
	 */
	@Override
	@Transactional
	public Resp updateResRoleRelation(List<String> resRoleIds, List<BaseResRoleResourceKey> resRoleResourceKeys) {
		Resp resp = new Resp();
	    resp.setCode(Constant.SUCCESS);
		roleResourceMapper.deleteByResRoleIds(resRoleIds);
		if(!resRoleResourceKeys.isEmpty()){
			roleResourceMapper.insertResRoleBatch(resRoleResourceKeys);
		}
		return resp;
	}

}
